package com.dykj.fireFighting.web.onenet.controller;

import com.dykj.fireFighting.web.onenet.service.HaiManService;
import com.dykj.fireFighting.web.utils.OneNetUtils;
import org.apache.dubbo.common.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping(value = "/onenet/haiman")
public class HaiManController {
    //海曼科技token
    private static String topriToken = "20211213";

    @Autowired
    private HaiManService haiManService;

    /**
     * 海曼烟感
     */
    @RequestMapping(value = "/receive/smoke",method = RequestMethod.POST)
    @ResponseBody
    public String receivetopri(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignatureTest(obj.getMsg().toString(),obj.getNonce(),obj.getMsgSignature(),topriToken);
            if (dataRight){
//                System.out.println("data receive: content" + obj.toString());
                haiManService.putHaiManSmokeToMQ(obj.getMsg(),"烟感");
            }else {
                System.out.println("data receive: signature error");
            }
        }else {
            System.out.println("data receive: body empty error");
        }
        return "ok";
    }
    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     */

    @RequestMapping(value = "/receive/smoke", method = RequestMethod.GET)
    @ResponseBody
    public String checktopri(@RequestParam(value = "msg") String msg,
                             @RequestParam(value = "nonce") String nonce,
                             @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
        boolean dataRight = OneNetUtils.checkSignatureTest(msg,nonce,signature, topriToken);
        System.out.println("dataRight-----------"+dataRight);
        return msg;
    }

    //================================================================================

    /**
     * 海曼烟感
     */
    @RequestMapping(value = "/receive/gas",method = RequestMethod.POST)
    @ResponseBody
    public String receiveGas(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignatureTest(obj.getMsg().toString(),obj.getNonce(),obj.getMsgSignature(),topriToken);
            if (dataRight){
//                System.out.println("data receive: content" + obj.toString());
                haiManService.putHaiManGasToMQ(obj.getMsg(),"可燃气体");
            }else {
                System.out.println("data receive: signature error");
            }
        }else {
            System.out.println("data receive: body empty error");
        }
        return "ok";
    }
    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     */

    @RequestMapping(value = "/receive/gas", method = RequestMethod.GET)
    @ResponseBody
    public String checkGas(@RequestParam(value = "msg") String msg,
                             @RequestParam(value = "nonce") String nonce,
                             @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
        boolean dataRight = OneNetUtils.checkSignatureTest(msg,nonce,signature, topriToken);
        System.out.println("dataRight-----------"+dataRight);
        return msg;
    }
}
